<?php

namespace App\Admin\Actions\NewsAction;

use App\Models\User;
use Illuminate\Support\Facades\DB;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Noweh\TwitterApi\Client;
use Exception;


class NewsExamine extends RowAction
{
    public $name = '审核';
    
    public function handle(Model $model)
    {
	    /**
	     * @var $model User
	     */
    	
    	if ($model->is_examine) {
    		return $this->response()->success('已审核过了');
	    }

	    //-------tiktok视频上传功能 -- 先上传再发布------------------//



	    //-------twitter视频上传功能 -- 先上传再发布------------------//
	   /* $settings = [
		    'account_id' => env('TWITTER_ACCOUNT_ID'),
		    'access_token' => env('TWITTER_ACCESS_TOKEN'),
		    'access_token_secret' => env('TWITTER_ACCESS_TOKEN_SECRET'),
		    'consumer_key' => env('TWITTER_CONSUMER_KEY'),
		    'consumer_secret' => env('TWITTER_CONSUMER_SECRET'),
		    'bearer_token' => env('TWITTER_BEARTOKEN'),
		    'free_mode' => false, // Optional
		    'api_base_uri' => 'https://api.x.com/2/', // Optional
		]; 

		$client = new Client($settings);

		$file_data = base64_encode(file_get_contents(img_path($model->images)));

		try{
		
			//$file_data = base64_encode(file_get_contents('https://xbreaking.oss-cn-beijing.aliyuncs.com/yuyin/b4c259c9e10759dd53bcb40ccc0aa0c0.mp4'));
			

			$media_info =$client->uploadMedia()->uploadVideo(file_get_contents('https://xbreaking.oss-cn-beijing.aliyuncs.com/yuyin/b4c259c9e10759dd53bcb40ccc0aa0c0.mp4'));
			var_dump($media_info);
			
			$return = $client->tweet()->create()
			    ->performRequest([
			        'text' => $model->title_zn, 
			        //'direct_message_deep_link'=>$model->url,
			        "media" => [
			            "media_ids" => [
			                (string)$media_info["media_id"]
			            ]
			        ]
			    ]);   	
    	} catch (Exception $e) {
           $message = $e->getCode().$e->getMessage();

           dd($message);

        }*/

         //-------twitter视频上传功能 -- END------------------//
    	//dd($return);
    	/*DB::transaction(function () use ($model) {
    		//dd($model->getTableName());
    		$table = $model->getTableName();
    		if($table =='deep_news'){
    			$client = new Client(
				    getenv('LINKDIN_CLIENT_ID'),
				    getenv('LINKDIN_CLIENT_SECRET'),
				);
				$client->setRedirectUrl('https://www.xbreakingtechinsight.com');

				$user = User::find(1);
				
				$client->setAccessToken($user->linkdin_token);
				$arr = [                                   
		                    'author' => 'urn:li:organization:100897090',
		                    'lifecycleState' => 'PUBLISHED',
		                    'specificContent' => [          
		                        'com.linkedin.ugc.ShareContent' => [
		                            'shareCommentary' => [
		                                'text' => $model->brief_zn
		                            ],
		                            'shareMediaCategory' => 'ARTICLE',
		                            'media' => [
		                                [
		                                    'status' => 'READY',
		                                    'description' => [
		                                        'text' => strip_tags($model->brief_zn)
		                                    ],
		                                    'thumbnails'=> [
		                                    	['url' => img_path($model->images)]
		                                    	
		                                    ],

		                                    //img_path($new['images']
		                                    'originalUrl' => "https://www.xbreakingtechinsight.com/detail/".$model->id,
		                                    'title' => [
		                                        'text' => $model->title_zn
		                                    ]
		                                ]
		                            ]
		                        ]
		                    ],
		                    'visibility' => [
		                        'com.linkedin.ugc.MemberNetworkVisibility' => 'PUBLIC'
		                    ]
		                ];

		               //var_dump($arr); die;
				$share = $client->post(                 
		                'ugcPosts',                         
		                $arr
		            );

				

    			
    		}*/
    		




		    $model->update([
			    'is_examine' => true,
			    //'linkedin_share_id'=>$share['id'],
		    ]);
	  //  });
    	
    	
	    return $this->response()->success('审核通过')->refresh();
    }
	
	

	
	public function dialog()
	{
		$this->confirm('确定审核通过吗?');
	}
}
